package com.hong610.net.http.client;

import java.io.File;
import java.util.List;

import com.hong610.net.config.Config;
import com.hong610.net.enums.NetAgreement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.CharsetUtils;

/**
 * 最原始请求，返回HttpResponse
 * Created by Hong on 2016/12/31.
 */
public abstract class NetHttpResponse extends Client {

	protected HttpResponse post() throws Exception {
		HttpClient httpClient = getHttpClient();
		HttpPost httpPost = new HttpPost(this.getUrl());
		httpPost.setConfig(getRequestConfig());
		return httpClient.execute(httpPost);
	}

	protected HttpResponse post(String parameterStream) throws Exception {
		HttpClient httpClient = getHttpClient();
		HttpPost httpPost = new HttpPost(this.getUrl());
		httpPost.setEntity(new StringEntity(parameterStream, charset));
		httpPost.setConfig(getRequestConfig());
		return httpClient.execute(httpPost);
	}
	
	protected HttpResponse post(List<BasicNameValuePair> params) throws Exception {
		HttpClient httpClient = getHttpClient();
		HttpPost httpPost = new HttpPost(this.getUrl());
		if (params != null && params.size() > 0) {
			UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(params, charset);
			httpPost.setEntity(uefEntity);
		}
		httpPost.setConfig(getRequestConfig());
		return httpClient.execute(httpPost);
	}

	protected HttpResponse get(String params) throws Exception {
		HttpClient httpClient = getHttpClient();
		HttpGet httpGet = new HttpGet(new StringBuffer(this.getUrl()).append(params == null ? "" : params).toString());
		httpGet.setConfig(getRequestConfig());
		return httpClient.execute(httpGet);
	}

	protected HttpResponse upload(File file) throws Exception {
		HttpClient httpClient = getHttpClient();
		HttpPost httpPost = new HttpPost(this.getUrl());
		
		FileBody fileBody = new FileBody(file);
		HttpEntity httpEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
			.addPart("file", fileBody)
			.setCharset(CharsetUtils.get("UTF-8"))
			.build();
		
		httpPost.setEntity(httpEntity);
		return httpClient.execute(httpPost);
	}

	private String getUrl(){
		if(netAgreement == NetAgreement.HTTP){
			return (url.startsWith("http://") ? url : String.format("http://%s", url));
		}else if(netAgreement == NetAgreement.HTTPS){
			return (url.startsWith("https://") ? url : String.format("https://%s", url));
		}else{
			return url;
		}
	}

	public RequestConfig getRequestConfig(){
		return RequestConfig.custom().setConnectTimeout(Config.TIMEOUT).build();
	}
}
